//#include <base.js>
//#include <openlayersExt.js>
//#include <event.js>

/**
* 
* Configurazione Applicazione
* 
* @namespace CWN2
* @class configuration
* 
*/

CWN2.configuration = (function (){
	
	// dependencies
	
	// private properties
	
	// private methods
	
	// ritorno l'oggetto config - Public API
	return {
		
		/**
		* @property name
	    * @type {string}
		*/
		name: "Configurazione CWN2",
		/**
		* @property mapOptions
	    * @type {mapOptions}
		*/
		mapOptions: {},
		/**
		* @property layout
	    * @type {layoutConfig}
		*/
		layout: {},
		/**
		* @property toolbar
	    * @type {toolbarConfig}
		*/
		toolbar: {},		
		/**
		* @property widgets
	    * @type [{widget}]
		*/
		widgets: [],		
		/**
		* @property legend
	    * @type {legend}
		*/
		legend: [],
		/**
		* @property layers
	    * @type [{layerConfig}]]
		*/
		layers: [],
		
		/**
		* 
		* Load a json containing the configuration
		* 
		* @method load
		* 
		* @param {String} url Url of the json configuration file
		* 
		* @return: {configuration}
		* 
		*/
		  
		load: function(url) {
		
			if(CWN2.base.DEBUG) {
				console.log('CWN2.configuration.load');
			} 
			
	
			// carico il Json
			var config = CWN2.json.load(url,true);
			
			// creo gli oggetti Bounds per le opzioni di tipo "Extent"
			var options = config.mapOptions;
			for (var option in options) {
				if ((option === "maxExtent") || (option === "restrictedExtent")) {
					options[option] = OpenLayers.Bounds.fromString(options[option]);
				}
			}

			// imposto le proprieta'
			this.name = config.name || this.name ;
			this.mapOptions = config.mapOptions;
			this.layout = config.layout;
			this.widgets = config.widgets;
			this.toolbar = config.toolbar;
			this.legend = config.legend;
			this.layers = config.layers;
		
			return this;
			
		},
		
		
		/**
		* 
		* Return the configuration of a layer with a given name
		* 
		* @method getLayerConfigByName
		* 
		* @param {String} layerName The name of the layer
		* 
		* @return 	{layerConfig}
		* 
		*/
		getLayerConfigByName: function (layerName) {
			var layers = CWN2.configuration.layers;
			var len = CWN2.configuration.layers.length;
			for (var i = 0; i < len; i++) {
				if (layers[i].name===layerName) {
					return layers[i];
				}
			}
		},
		
		/**
		* 
		* Return the configuration of a toolbar item with a given type
		* 
		* @method getToolbarItemConfigByType
		* 
		* @param {String} itemType The type of the item
		* 
		* @return 	{toolbarItemConfig}
		* 
		*/
		getToolbarItemConfigByType: function (itemType) {

			for (var group in CWN2.configuration.toolbar.itemGroups) {
				for (var item in CWN2.configuration.toolbar.itemGroups[group].items) {
					if (CWN2.configuration.toolbar.itemGroups[group].items[item].type === itemType) {
						return CWN2.configuration.toolbar.itemGroups[group].items[item];
					}
				}
			}			
			
		},
		
		/**
		* 
		* Return the configuration of a widget with a given type
		* 
		* @method getWidgetConfigByName
		* 
		* @param {String} widgetName The name of the item
		* 
		* @return 	{widgetConfig}
		* 
		*/
		getWidgetConfigByName: function (widgetName) {

			var widgets = CWN2.configuration.widgets;
			var len = widgets.length;
			for (var i = 0; i < len; i++) {
				if (widgets[i].name === widgetName) {
					return widgets[i];
				}
			}			
			
		}		
	};
}());

